package quickcore.webview;

import android.net.Uri;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class WebChromeClientImpl extends WebChromeClient{
    private WebViewListener listener;

    public WebChromeClientImpl(WebViewListener listener) {
        this.listener = listener;
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        listener.setTitleView(title);
    }

    @Override
    public void onGeolocationPermissionsShowPrompt(String s, GeolocationPermissions.Callback geolocationPermissionsCallback) {
        geolocationPermissionsCallback.invoke(s, true, false);
        super.onGeolocationPermissionsShowPrompt(s, geolocationPermissionsCallback);
    }

    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        listener.onOpenFile(true,filePathCallback,null);
        return true;
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        listener.onOpenFile(false,null,uploadMsg);
    }

    // For Android  > 4.1.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        listener.onOpenFile(false,null,uploadMsg);
    }
}
